<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑角色</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css" />
    <script src="/component/layui/layui.js"></script>
    <script src="/component/pear/pear.js"></script>
</head>
<body>
    <form class="layui-form" action="" lay-filter="role-form">
        <div class="top">
            <div class="button-container">
                <div style="padding: 10px;">
                    <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit lay-filter="role-save">
                        <i class="layui-icon layui-icon-ok"></i>
                        提交
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </div>
        <div class="mainBox">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label required">角色名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" lay-verify="required" lay-reqtext="角色名称不能为空" 
                            placeholder="请输入角色名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">角色编码</label>
                    <div class="layui-input-block">
                        <input type="text" name="code" lay-verify="required" lay-reqtext="角色编码不能为空" 
                            placeholder="请输入角色编码" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="1" title="正常" checked>
                        <input type="radio" name="status" value="0" title="禁用">
                    </div>
                </div>
            </div>
        </div>
    </form>
    <style>
    .top {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        background-color: #fff;
        box-shadow: 0 1px 4px rgba(0,21,41,.08);
        z-index: 999;
    }
    .mainBox {
        margin-top: 55px;
        padding: 15px;
    }
    .main-container {
        background: #fff;
        padding: 15px 30px;
        border-radius: 4px;
        box-shadow: 0 1px 4px rgba(0,21,41,.08);
    }
    .button-container {
        text-align: left;
        padding: 0 15px;
    }
    .layui-btn {
        padding: 0 25px;
        height: 35px;
        line-height: 35px;
        margin: 0 5px;
    }
    .layui-form-item {
        margin-bottom: 25px;
        position: relative;
    }
    .layui-input {
        height: 38px;
        line-height: 38px;
        border-radius: 4px;
    }
    </style>
    <script>
        layui.use(['form', 'jquery', 'popup'], function() {
            let form = layui.form;
            let $ = layui.jquery;
            let popup = layui.popup;

            let id = getUrlParam("id");
            if (id != null) {
                $.ajax({
                    url: '/api/role/' + id,
                    dataType: 'json',
                    type: 'get',
                    success: function(result) {
                        if (result.code == 0) {
                            form.val('role-form', result.data);
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            }

            form.on('submit(role-save)', function(data) {
                let loading = layer.load();
                $.ajax({
                    url: id == null ? '/api/role' : '/api/role/' + id,
                    data: JSON.stringify(data.field),
                    dataType: 'json',
                    contentType: 'application/json',
                    type: id == null ? 'post' : 'put',
                    success: function(result) {
                        layer.close(loading);
                        if (result.code == 0) {
                            popup.success(result.msg, function() {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                                parent.window.refresh();
                            });
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
                return false;
            });
        });

        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        }
    </script>
</body>
</html> 